/// 用户信息模型
class UserInfo {
  final String id;
  final String username;
  final String email;
  final String phoneNumber;
  final String? avatar;
  final String status;
  final String role;
  final String nickname;

  UserInfo({
    required this.id,
    required this.username,
    required this.email,
    required this.phoneNumber,
    this.avatar,
    required this.status,
    required this.role,
    required this.nickname,
  });

  factory UserInfo.fromJson(Map<String, dynamic> json) {
    return UserInfo(
      id: json['id']?.toString() ?? '',
      username: json['username']?.toString() ?? '',
      email: json['email']?.toString() ?? '',
      phoneNumber: json['phone_number']?.toString() ?? '',
      avatar: json['avatar']?.toString(),
      status: json['status']?.toString() ?? 'active',
      role: json['role']?.toString() ?? 'user',
      nickname: json['nickname']?.toString() ?? '',
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'username': username,
      'email': email,
      'phone_number': phoneNumber,
      'avatar': avatar,
      'status': status,
      'role': role,
      'nickname': nickname,
    };
  }

  @override
  String toString() {
    return 'UserInfo{id: $id, username: $username, email: $email, phoneNumber: $phoneNumber}';
  }
}

/// 登录数据模型
class LoginData {
  final UserInfo user;
  final String token;
  final int expiresIn;

  LoginData({
    required this.user,
    required this.token,
    required this.expiresIn,
  });

  factory LoginData.fromJson(Map<String, dynamic> json) {
    return LoginData(
      user: UserInfo.fromJson(json['user'] as Map<String, dynamic>),
      token: json['token']?.toString() ?? '',
      expiresIn: (json['expires_in'] as num?)?.toInt() ?? 0,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'user': user.toJson(),
      'token': token,
      'expires_in': expiresIn,
    };
  }
}

/// 登录响应模型
class LoginResponse {
  final bool success;
  final String message;
  final LoginData? data;

  LoginResponse({
    required this.success,
    required this.message,
    this.data,
  });

  factory LoginResponse.fromJson(Map<String, dynamic> json) {
    return LoginResponse(
      success: json['success'] as bool? ?? false,
      message: json['message']?.toString() ?? '',
      data: json['data'] != null 
          ? LoginData.fromJson(json['data'] as Map<String, dynamic>)
          : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'success': success,
      'message': message,
      'data': data?.toJson(),
    };
  }
}

/// 发送验证码请求模型
class SendCodeRequest {
  final String phoneNumber;

  SendCodeRequest({required this.phoneNumber});

  Map<String, dynamic> toJson() {
    return {
      'phone_number': phoneNumber,
    };
  }
}

/// 验证码验证请求模型
class VerifyCodeRequest {
  final String phoneNumber;
  final String code;
  final String verificationId;

  VerifyCodeRequest({
    required this.phoneNumber,
    required this.code,
    required this.verificationId,
  });

  Map<String, dynamic> toJson() {
    return {
      'phone_number': phoneNumber,
      'code': code,
      'verification_id': verificationId,
    };
  }
}

/// API通用响应模型
class ApiResponse<T> {
  final bool success;
  final String message;
  final T? data;

  ApiResponse({
    required this.success,
    required this.message,
    this.data,
  });

  factory ApiResponse.fromJson(
    Map<String, dynamic> json,
    T Function(dynamic)? fromJsonT,
  ) {
    return ApiResponse<T>(
      success: json['success'] as bool? ?? false,
      message: json['message']?.toString() ?? '',
      data: json['data'] != null && fromJsonT != null 
          ? fromJsonT(json['data'])
          : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'success': success,
      'message': message,
      'data': data,
    };
  }
}